//
//  SourceView.swift
//  Box
//
//  Created by mn on 2025/3/28.
//

import UIKit
import SnapKit

class SourceView: UIView {
    private let colorIndicator: UIView = {
        let view = UIView()
        view.layer.cornerRadius = 3
        view.layer.masksToBounds = true
        return view
    }()
    
    private let textLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 9)
        label.textColor = .white
        return label
    }()
    
    init(text: String, color: UIColor) {
        super.init(frame: .zero)
        backgroundColor = UIColor.fromHex("#232321")
        layer.cornerRadius = 4
        layer.masksToBounds = true
        
        addSubview(colorIndicator)
        addSubview(textLabel)
        
        colorIndicator.backgroundColor = color
        textLabel.text = text
        
        colorIndicator.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(4)
            make.centerY.equalToSuperview()
            make.size.equalTo(CGSize(width: 6, height: 6))
        }
        
        textLabel.snp.makeConstraints { make in
            make.left.equalTo(colorIndicator.snp.right).offset(3)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(6.5)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(text: String, color: UIColor) {
        textLabel.text = text
        colorIndicator.backgroundColor = color
    }
}
